{*
  Object Pascal simple number, identifier validation routines
  @Author  Maciej Zagozda (1998-2008)
  @Licence Microsoft Reciprocal License (Ms-RL)
  @Website http://www.codeplex.com/intelitechdelphirtns/
  @Version 2008/04/20 Added documentation support
}
unit ValidationRoutines;

interface

uses
  Classes,
  Controls,
  Dialogs,                
  ExtCtrls,
  Forms,
  Graphics,
  Messages,
  SysUtils,
  Windows;


function ValidateAccountCode( strnAccountCode: AnsiString ): Boolean;
function ValidateNIPCode( strnNIPCode: AnsiString ): Boolean;
function ValidatePESELCode( strnPESELCode: AnsiString ): Boolean;
function ValidateREGONCode( strnREGONCode: AnsiString ): Boolean;

implementation

function ValidateAccountCode( strnAccountCode: AnsiString ): Boolean;
const
  strnPattern = '71397113';
var
  intgA, intgX, intgY: Byte;
  intgZ, intgV: Word;

begin
  strnAccountCode := Copy( strnAccountCode, 1, 8 );
  intgV := 0;
  for intgA := 1 to 7 do
  begin
    try
      intgX := StrToInt( Copy( strnAccountCode, intgA, 1 ));
      intgY := StrToInt( Copy( strnPattern, intgA, 1 ));
    except
      Result := False;
      Exit;
    end;//try

    intgZ := intgX * intgY;
    intgV := intgV + intgZ;
  end;//for

  try
    Result := StrToInt( Copy( strnAccountCode, 8, 1 )) = ( intgV mod 10 );
  except
    Result := False;
  end;//try

end;

function ValidateNIPCode( strnNIPCode: AnsiString ): Boolean;
const
  strnPattern = '657234567';
var
  intgA, intgX, intgY: Byte;
  intgZ, intgV: Word;

begin
  strnNIPCode := StringReplace( strnNIPCode, '-', '', [rfReplaceAll] );
  if Length( strnNIPCode ) <> 10 then
  begin
    Result := False;
    Exit;
  end
  else
  begin
    intgV := 0;
    for intgA := 1 to 9 do
    begin
      try
        intgX := StrToInt( Copy( strnNIPCode, intgA, 1 ));
        intgY := StrToInt( Copy( strnPattern, intgA, 1 ));
      except
        Result := False;
        Exit;
      end;//try

      intgZ := intgX * intgY;
      intgV := intgV + intgZ;
    end;//for

    try
      Result := StrToInt( Copy( strnNIPCode, 10, 1 )) = ( intgV mod 11 );
    except
      Result := False;
    end;//try

  end;//if
end;

function ValidatePESELCode( strnPESELCode: AnsiString ): Boolean;
const
  strnPattern = '1379137913';
var
  intgA, intgX, intgY: Byte;
  intgZ, intgV: Word;

begin
  if Length( strnPESELCode ) <> 11 then
  begin
    Result := False;
    Exit;
  end
  else
  begin
    intgV := 0;
    for intgA := 1 to 10 do
    begin
      try
        intgX := StrToInt( Copy( strnPESELCode, intgA, 1 ));
        intgY := StrToInt( Copy( strnPattern, intgA, 1 ));
      except
        Result := False;
        Exit;
      end;//try

      intgZ := intgX * intgY;
      intgV := intgV + intgZ;
    end;//for

    intgV := intgV mod 10;

    try
      Result := StrToInt( Copy( strnPESELCode, 11, 1 )) = ( 10 - intgV );
    except
      Result := False;
    end;//try

  end;//if
end;

function ValidateREGONCode( strnREGONCode: AnsiString ): Boolean;
const
  strnPattern = '89234567';
var
  intgA, intgX, intgY: Byte;
  intgZ, intgV: Word;

begin
  if Length( strnREGONCode ) <> 9 then
  begin
    Result := False;
    Exit;
  end
  else
  begin
    intgV := 0;
    for intgA := 1 to 8 do
    begin
      try
        intgX := StrToInt( Copy( strnREGONCode, intgA, 1 ));
        intgY := StrToInt( Copy( strnPattern, intgA, 1 ));
      except
        Result := False;
        Exit;
      end;//try

      intgZ := intgX * intgY;
      intgV := intgV + intgZ;
    end;//for

    try
      Result := StrToInt( Copy( strnREGONCode, 9, 1 )) = ( intgV mod 11 );
    except
      Result := False;
    end;//try

  end;//if
end;


end.
